<template>
  <div>
    <el-upload
      action="#"
      :file-list="fileList"
      list-type="picture-card"
      :class="{disable: fileList.length > 0}"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :on-change="handleChange"
      :before-upload="beforeUpload"
      :http-request="upload"
    >
      <i slot="default" class="el-icon-plus" />
    </el-upload>
    <el-progress v-if="showProgress" :show-text="false" :percentage="percentage" class="progressBar" />
    <el-dialog
      title="图片详情"
      :visible="showDialog"
      @close="showDialog = false"
    >
      <img :src="imgUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDKRRNCqMQadZvUxlMFwPdDsHO9zaxF0rV',
  SecretKey: 'OoxtZ3KPadyhJepFRNhQgiV2LRHwpU4U'
})
export default {
  data() {
    return {
      showProgress: false,
      percentage: 0,
      currentFileUid: '',
      showDialog: false,
      imgUrl: '',
      fileList: [
        {
          url:
            'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1320098600,3499910052&fm=26&gp=0.jpg'
        }
      ]
    }
  },
  methods: {
    upload(params) {
      // console.log(params)
      if (params.file) {
        cos.putObject({
          Bucket: 'hr-1305135837', // 存储桶
          Region: 'ap-guangzhou', // 地域
          Key: params.file.name, // 文件名
          Body: params.file, // 要上传的文件对象
          StorageClass: 'STANDARD', // 上传的模式类型 直接默认 标准模式即可
          onProgress: (progressData) => {
            this.percentage = progressData.percent * 100
          }
          // 上传到腾讯云 =》 哪个存储桶 哪个地域的存储桶 文件  格式  名称 回调
        }, (err, data) => {
          // data返回数据之后 应该如何处理
          if (data.statusCode === 200) {
            this.fileList = this.fileList.map(item => {
              if (item.uid === this.currentFileUid) {
                item.url = 'http://' + data.Location
                item.status = 'success'
              }
              return item
            })
            setTimeout(() => {
              this.showProgress = false
            }, 1000)
          } else {
            console.log(err)
          }
        })
      }
    },
    handlePreview(file) {
      this.showDialog = true
      this.imgUrl = file.url
      console.log(file)
    },
    handleRemove(file, fileList) {
      // console.log(file)
      // console.log(fileList)
      this.fileList = [...fileList]
    },
    handleChange(file, fileList) {
      // console.log(fileList)
      this.fileList = [...fileList]
    },
    beforeUpload(file) {
      // console.log(file)
      const types = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png']
      if (!types.includes(file.type)) {
        this.$message.error('上传图片只能是 JPG、GIF、BMP、PNG 格式!')
        return false
      }
      const maxSize = 5 * 1024 * 1024
      if (maxSize < file.size) {
        this.$message.error('图片大小最大不能超过5M')
        return false
      }
      console.log('file', file)
      this.currentFileUid = file.uid
      this.showProgress = true
      return true
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep img {
  object-fit: cover;
  max-width: 100%;
  width: 100%;
}
.disable {
  ::v-deep .el-upload {
    display: none;
  }
}
.progressBar {
  width: 146px;
}
</style>
